<?php

// phpMyRealty 3
//
// File Name: comparelistings.php
// File Location : ./
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// Include configuration file and general functions
define('PMR', 'true');

$page = 'compare';

include ('./config.php');
include (PATH . '/defaults.php');

// Title tag content
$title = $conf['website_name_short'] . ' - ' . $lang['Compare_Listings'];

// Template header
include ( PATH . '/templates/' . $cookie_template . '/header.php' );

$error = 0;

if (!isset($_POST['compare'])
    || !is_array($_POST['compare'])
    || count($_POST['compare']) < 2
    || count($_POST['compare']) > 3) {

    echo $lang['Compare_Error'];
    $error = 1;
}

if (!$error)

 {

  $ids = '';

  foreach ($_POST['compare'] as $key => $id)
   {
    $ids .= '"' . intval($id) . '", ';
   }

  $ids .= '"0"';

	// Title/descr language to use (if available)
	$title = str_replace( 'name', 'title', $language_in );
	$description = str_replace( 'name', 'description', $language_in );

    $sql = 'SELECT ' . $title . ', ' . $description . ', ' . PROPERTIES_TABLE . '.* FROM ' . PROPERTIES_TABLE  . ' WHERE id IN ( ' . $ids . ' )';

    // End creating sql query

    echo table_header ( $lang['Compare_Listings'] );

    echo '
     <table width="100%" cellpadding="5" cellspacing="0" border="0"><tr>
         ';

    // Fetch number of results per page
    $r2 = $db->query($sql) or error ('Critical Error', mysql_error ());

    $i = 0;

    $tpl = implode ('', file( PATH . '/templates/' . $cookie_template . '/tpl/property_compare.tpl' ));

    while ($f = $db->fetcharray( $r2 ))
     {

      $f = array_map ( 'if_empty', $f);

      // Starting a new template
      $template = new Template;

      // Load user short search results template
      $template->load ( $tpl );

	// Default
	if ($f[0] == '')
		$f['title'] = $f['title'];
	else
		$f['title'] = $f[0];

	if ($f[1] == '')
		$f['description'] = $f['description'];
	else
		$f['description'] = $f[1];

      if ($db->numrows($r2) == 2)
       $template->set ( 'width', '50%' );
      else
       $template->set ( 'width', '33%' );

      $sql = 'SELECT * FROM ' . USERS_TABLE  . ' WHERE id = ' . $f['userid'] . ' LIMIT 1';
      $r_user = $db->query ( $sql );
      $f_user = $db->fetcharray ( $r_user );

      if ($f_user['package'] != '0' && $f_user['package'] != '')
       {

        $sql = 'SELECT * FROM ' . PACKAGES_AGENT_TABLE  . ' WHERE id = ' . $f_user['package'] . ' LIMIT 1';
        $r_package = $db->query ( $sql );
        $f_package = $db->fetcharray ( $r_package );

       }

      else

       {

        $f_package['listings'] = $conf['free_listings'];
        $f_package['gallery'] = $conf['free_gallery'];
        $f_package['mainimage'] = $conf['free_mainimage'];
        $f_package['photo'] = $conf['free_photo'];
        $f_package['phone'] = $conf['free_phone'];
        $f_package['address'] = $conf['free_address'];

       }

      // VALUES

      // Replace the template variables

      $template->set ( 'print', '<a href="' . URL . '/print.php?id=' . $f['id'] . '"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/print.png" border="0" alt=""><br />' . $lang['Listing_Print'] . '</a>' );

      if ($f_package['mainimage'] == 'ON')
       $template->set ( 'image', show_gallery_image ('images', $f['id']) );
      else
       $template->set ( 'image', '' );

      $template->set ( 'mls', $f['mls'] );
      $template->set ( 'title', $f['title'] );
      $template->set ( 'type', getnamebyid ( TYPES_TABLE, $f['type'] ) );
      $template->set ( 'type2', getnamebyid ( TYPES2_TABLE, $f['type2'] ) );
      $template->set ( 'status', getnamebyid ( STATUS_TABLE, $f['status'] ) );
      $template->set ( 'style', getnamebyid ( STYLES_TABLE, $f['style'] ) );
      $template->set ( 'description', removehtml(unsafehtml($f['description'])) );
      $template->set ( 'lot_size', $f['size'] );
      $template->set ( 'dimensions', $f['dimensions'] );
      $template->set ( 'bathrooms', $f['bathrooms'] );
      $template->set ( 'half_bathrooms', $f['half_bathrooms'] );
      $template->set ( 'bedrooms', $f['bedrooms'] );

      $cat = explode ("#", $f['location2']);
      $r11 = $db->query('SELECT category FROM ' . LOCATION1_TABLE . ' WHERE selector = "' . $cat[0] . '"');
      $f11 = $db->fetcharray($r11);
      $template->set ( 'location1', $f11['category'] );

      $r22 = $db->query('SELECT subcategory FROM ' . LOCATION2_TABLE . ' WHERE catsubsel = "' . $cat[1] . '"');
      $f22 = $db->fetcharray($r22);
      $template->set ( 'location2', $f22['subcategory'] );

      $r33 = $db->query('SELECT subsubcategory FROM ' . LOCATION3_TABLE . ' WHERE catsubsubsel = "' . $cat[2] . '"');
      $f33 = $db->fetcharray($r33);
      $template->set ( 'location3', $f33['subsubcategory'] );

      // Do not show address if it is not allowed in the listing
      if ($f['display_address'] == 'YES' &&  $f_package['address'] == 'ON')
       {
        $template->set ( 'address1', $f['address1'] );
        $template->set ( 'address2', $f['address2'] );
        $template->set ( 'zip', $f['zip'] );
       }
      elseif ($f['display_address'] != 'YES' || $f_package['address'] != 'ON')
       {
        $template->set ( 'address1', ' ' );
        $template->set ( 'address2', ' ' );
        $template->set ( 'zip', ' ' );
       }

      $template->set ( 'price', pmr_number_format($f['price']) );
      $template->set ( 'currency', $conf['currency'] );

      if ($f['display_address'] == 'YES') $directions_address = urlencode($f['address1'] . ' ' . $f['address2']);
      else $directions_address = '';

      $directions_country = urlencode($f11['category']);
      $directions_city = urlencode($f33['subsubcategory']);
      $directions_state = urlencode($f22['subcategory']);

      $directions = '<a href="http://maps.yahoo.com/dd;_ylt=AqITujRB0pfczOh.0A59Fnga3kcC?taddr=' . $directions_address . '&amp;tcsz=' . $directions_city . ' ' . $directions_state . '&amp;tcountry=' . $directions_country . '" target="new">' . $lang['Listing_Yahoo_Directions'] . ' </a>';

      $template->set ( 'directions', $f['directions'] . ' <br /> ' . $directions );

      unset($directions);
      // DIRECTIONS : END

      // Mail to a friend
      if ($conf['rewrite'] == 'ON')
       $template->set ( 'friend', '<a href="' . URL . '/Friend/' . $f['id'] . '.html"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/friend.png" border="0" alt=""><br />' . $lang['Mail_Friend'] . '</a>' );
      else
       $template->set ( 'friend', '<a href="' . URL . '/friend.php?id=' . $f['id'] . '"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/friend.png" border="0" alt=""><br />' . $lang['Mail_Friend'] . '</a>' );

      $template->set ( 'year_built', $f['year_built'] );
      $template->set ( 'buildings', show_multiple ( BUILDINGS_TABLE, $f['buildings'] ) );
      $template->set ( 'appliances', show_multiple ( APPLIANCES_TABLE, $f['appliances'] ) );
      $template->set ( 'features', show_multiple ( FEATURES_TABLE, $f['features'] ) );
      $template->set ( 'garage', getnamebyid ( GARAGE_TABLE, $f['garage'] ) );
      $template->set ( 'garage_cars', $f['garage_cars'] );
      $template->set ( 'basement', getnamebyid ( BASEMENT_TABLE, $f['basement'] ) );
      $template->set ( 'date_added', printdate($f['date_added']) );
      $template->set ( 'date_updated', printdate($f['date_updated']) );
      $template->set ( 'date_upgraded', printdate($f['date_upgraded']) );
      $template->set ( 'ip_added', $f['ip_added'] );
      $template->set ( 'ip_updated', $f['ip_updated'] );
      $template->set ( 'ip_upgraded', $f['ip_upgraded'] );
      $template->set ( 'hits', $f['hits'] );

      // Slide Show JS
      $slideshowJS = '   var slideCache = new Array();   function RunSlideShow(pictureName,imageFiles,displaySecs)    {	var imageSeparator = imageFiles.indexOf(";");	var nextImage = imageFiles.substring(0,imageSeparator);	if (document.all)	{		document.getElementById(pictureName).style.filter=			"blendTrans(duration=1)";		document.getElementById(pictureName).filters.blendTrans.Apply();	}	document.getElementById(pictureName).src = nextImage;	if (document.all)	{		document.getElementById(pictureName).filters.blendTrans.Play();	}	var futureImages= imageFiles.substring(imageSeparator+1,imageFiles.length)		+ \';\' + nextImage + \'\';	setTimeout("RunSlideShow(\'"+pictureName+"\',\'"+futureImages+"\',"+displaySecs+")",		displaySecs*1000); imageSeparator = futureImages.indexOf(";");	nextImage = futureImages.substring(0,imageSeparator);	if (slideCache[nextImage] == null) {		slideCache[nextImage] = new Image;		slideCache[nextImage].src = nextImage;	}		   }  ';

      // Fetch image names from the photo gallery
      $sql = 'SELECT * FROM ' . GALLERY_TABLE . ' WHERE listingid = "' . $f['id'] . '" ORDER BY id';
      $r_gallery = $db->query( $sql ) or error ('Critical Error', mysql_error ());

      $gallery_images = '';

      // is some images found we start
      if ( $db->numrows($r_gallery) > 0)

       {

        // Fetch the first image
        $f_gallery = $db->fetcharray($r_gallery);

        if (file_exists( PATH . '/gallery/' . $f_gallery['id'] . '.jpg'))
         $main_image = URL . '/gallery/' . $f_gallery['id'] . '.jpg';
        else
         $main_image = '';

        // Create a list of images
        while ($f_gallery = $db->fetcharray($r_gallery))
         {

          if (file_exists( PATH . '/gallery/' . $f_gallery['id'] . '.jpg'))
           $gallery_images .=  ';' . URL . '/gallery/' . $f_gallery['id'] . '.jpg';

         }

        if ($gallery_images == '')
         $gallery_images = ';' . $main_image;

        // Output the slide show JS with the images included
        if ($main_image != '')
         $template->set ( 'slideshow' , '<script language="JavaScript" type="">' . $slideshowJS . '</script><img border="1" src="' . $main_image . '" width="320" height="240" id="PMR" alt=""><script language="JavaScript" type="">RunSlideShow("PMR","' . $main_image . $gallery_images . '",3);</script>');
        else
         $template->set ( 'slideshow' , '');

       }

      else

       // If no images found we show nothing
       $template->set ( 'slideshow' , '');

      // MAP
      // Show map of the city or state if address is not allowed to be
      // shown

      if ($f['display_address'] == 'YES')
       $mapaddr = $f['address1'] . ' ' . $f['address2'] . ' ' . $f33['subsubcategory'] . ' ' . $f22['subcategory'] . ' ' . $f11['category'];
      else
       $mapaddr = $f33['subsubcategory'] . ' ' . $f22['subcategory'] . ' ' . $f11['category'];

      // Cleans the address data to pass it to the new script
      $mapaddr = str_replace ('n/a', '', $mapaddr);
      $mapaddr = ereg_replace (' +', ' ', $mapaddr);
      $mapaddr = urlencode ($mapaddr);

      // Will open the map in the new window
      $template->set ( 'map' , '<a href="JavaScript:windowOpen(\'' . URL . '/map.php?address=' . $mapaddr . '\', 500, 500)"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/map.png" border="0" alt=""><br />' . $lang['View_Map'] . '</a>');
      unset ($mapaddr);

      // MAP : END
      // View realtor name
      $sql = 'SELECT * FROM ' . USERS_TABLE  . ' WHERE approved = 1 AND id = ' . $f['userid'] . ' LIMIT 1';
      $r_user = $db->query ( $sql ) or error ('Critical Error' , mysql_error());
      $f_user = $db->fetcharray ($r_user);

      if ($conf['rewrite'] == 'ON')
       $template->set ( 'view_realtor', '<a href="' . URL . '/Realtor/' . $f['userid'] . '.html">' . $f_user['first_name'] . ' ' . $f_user['last_name'] . '</a>'  );
      else
       $template->set ( 'view_realtor', '<a href="' . URL . '/viewuser.php?id=' . $f['userid'] . '">' . $f_user['first_name'] . ' ' . $f_user['last_name'] . '</a>');

      if (favoriteListingsCheck($f['id']) === FALSE)
       $template->set ( 'favorites', '<a href="' . URL . '/viewlisting.php?id=' . $f['id'] . '&amp;favorites=ADD"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/add-favorite.png" border="0" alt=""><br />' . $lang['Favorites_Add'] . ' </a>');

      if (favoriteListingsCheck($f['id']) >= 0)
       $template->set ( 'favorites', '<a href="' . URL . '/viewlisting.php?id=' . $f['id'] . '&amp;favorites=REMOVE"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/add-favorite.png" border="0" alt=""><br />' . $lang['Favorites_Remove'] . ' </a>');

      // Relator Details

      $template->set ( 'realtor_first_name', $f_user['first_name'] );
      $template->set ( 'realtor_last_name', $f_user['last_name'] );
      $template->set ( 'realtor_company_name', $f_user['company_name'] );

      if ($f_package['phone'] == 'ON')
       {
        $template->set ( 'realtor_phone', $f_user['phone'] );
        $template->set ( 'realtor_fax', $f_user['fax'] );
        $template->set ( 'realtor_mobile', $f_user['mobile'] );
       }
      else
       {
        $template->set ( 'realtor_phone', '' );
        $template->set ( 'realtor_fax', '' );
        $template->set ( 'realtor_mobile', '' );
       }

      $template->set ( 'realtor_mail', validateemail ( $f['userid'], $f_user['email'] ) . ' ' . $lang['Realtor_Send_Message'] . '</a>' );

      // PAGERANK END

      $template->set ( 'realtor_website', validatewebsite ( $f['userid'], $f_user['website'] ) . ' ' . $lang['Realtor_Visit_Website'] . '</a><br />');

      $template->set ( 'video', validatewebsite ( $f['id'], $f['video'] ) . '<img src="' . URL . '/templates/' . $cookie_template . '/images/icons/video.png" border="0" alt=""><br />' . $lang['Video_Tour'] . '</a>');

      $template->set ( 'pdf', '<a href="' . URL . '/pdf.php?id=' . $f['id'] . '" target="new"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/pdf.gif" border="0" alt=""><br />PDF</a>' );

      $template->set ( 'new', newitem ( PROPERTIES_TABLE, $f['id'], $conf['new_days']) );
      $template->set ( 'updated', updateditem ( PROPERTIES_TABLE, $f['id'], $conf['updated_days']) );
      $template->set ( 'featured', featureditem ( $f['featured'] ) );



      // Names

      $template->set ( '@mls', $lang['Listing_MLS'] );
      $template->set ( '@title', $lang['Listing_Title'] );
      $template->set ( '@type', $lang['Listing_Property_Type'] );
      $template->set ( '@style', $lang['Listing_Style'] );
      $template->set ( '@status', $lang['Listing_Status'] );
      $template->set ( '@description', $lang['Listing_Description'] );
      $template->set ( '@lot_size', $lang['Listing_Lot_Size'] );
      $template->set ( '@dimensions', $lang['Listing_Dimensions'] );
      $template->set ( '@bathrooms', $lang['Listing_Bathrooms'] );
      $template->set ( '@half_bathrooms', $lang['Listing_Half_Bathrooms'] );
      $template->set ( '@bedrooms', $lang['Listing_Bedrooms'] );
      $template->set ( '@location', $lang['Search_Location'] );
      $template->set ( '@city', $lang['City'] );
      $template->set ( '@address1', $lang['Listing_Address1'] );
      $template->set ( '@address2', $lang['Listing_Address2'] );
      $template->set ( '@zip', $lang['Zip_Code'] );
      $template->set ( '@price', $lang['Listing_Price'] );
      $template->set ( '@directions', $lang['Listing_Directions'] );
      $template->set ( '@year_built', $lang['Listing_Year_Built'] );
      $template->set ( '@buildings', $lang['Listing_Additional_Out_Buildings'] );
      $template->set ( '@appliances', $lang['Listing_Appliances_Included'] );
      $template->set ( '@features', $lang['Listing_Features'] );
      $template->set ( '@garage', $lang['Listing_Garage'] );
      $template->set ( '@garage_cars', $lang['Listing_Garage_Cars'] );
      $template->set ( '@basement', $lang['Listing_Basement'] );

      $template->set ( '@date_added', $lang['Date_Added'] );
      $template->set ( '@date_updated', $lang['Date_Updated'] );
      $template->set ( '@date_upgraded', $lang['Date_Upgraded'] );

      $template->set ( '@hits', $lang['Hits'] );
      $template->set ( '@view_realtor', $lang['View_Realtor'] );
      $template->set ( '@image_url', URL . '/templates/' . $cookie_template . '/images' );

      //

      // Set background color
      $bgcolor = ''; // Background color for all odd listings
      $bgcolor2 = $conf['list_background_color_even']; // Background color for all even listings

      if ( $i%2 == 0 )
       $template->set ( 'bgcolor', $bgcolor );
      else
       $template->set ( 'bgcolor', $bgcolor2 );

      // Publish template
      $template->publish();

      $i++;
  }

    echo '
     </tr></table>';

    echo table_footer ();

 }

// Template footer
include ( PATH . '/templates/' . $cookie_template . '/footer.php' );

?>